

function init_posent() {
	var dwrCallMetaData = {
		callback : success_findpobysupp,
		exceptionHandler : exception_generic_handler,
		timeout : 100000,
		errorHandler : exception_generic_handler
	};

	DWR_PurchaseOrders.fetchPurchaseOrdersForCompany(usrcompany_uid,true,false,
			dwrCallMetaData);
}

var customAttachmentFormatter = function(elLiner, oRecord, oColumn, oData) {
    if(oRecord.getData("orderAttachments") != null &&  typeof oRecord.getData("orderAttachments") != 'undefined' ) {
    	var attachment = oRecord.getData("orderAttachments")[0];
        elLiner.innerHTML = "<a href="+attachment.uniqueName+">"+ 'Download'+ "</a>";
    }
    else {
    	  elLiner.innerHTML = "N/A";
    }
};

var posent_cache = null;

function success_findpobysupp(dwrresp) {
	pos_yui_table_instance = create_pos_results_table(pos_yui_table_instance,"posent_yui_table",poscolumnDefs);
	pos_yui_table_instance_forchnage = create_pos_results_table(pos_yui_table_instance_forchnage,"open_send_POTable",poscolumnDefs);
	
	clear_pos_results_table(pos_yui_table_instance) ;
	clear_pos_results_table(pos_yui_table_instance_forchnage) ;
	pos_yui_table_instance.render();
	pos_yui_table_instance_forchnage.render();
	if (dwrresp && dwrresp.respCode == 0) {
		if (dwrresp.objRespOrError != null && dwrresp.objRespOrError["PO_SENT"] != null && dwrresp.objRespOrError["PO_SENT"].length>0) {
			posent_cache = dwrresp.objRespOrError["PO_SENT"];
			//orderAttachments
			pos_yui_table_instance.addRows(dwrresp.objRespOrError["PO_SENT"]);
			pos_yui_table_instance.render();
			
			pos_yui_table_instance_forchnage.addRows(dwrresp.objRespOrError["PO_SENT"]);
			
			pos_yui_table_instance_forchnage.render();
			//set the sort column
			var col = pos_yui_table_instance.getColumn('orderNumber');
			var col_forchange = pos_yui_table_instance_forchnage.getColumn('orderNumber');
			//sort records here
			pos_yui_table_instance.sortColumn(col,YAHOO.widget.DataTable.CLASS_DESC );
			pos_yui_table_instance_forchnage.sortColumn(col_forchange,YAHOO.widget.DataTable.CLASS_DESC );
		}
		$("po_sent_throbber").className = "throbberOff";
	}
	
	//create_PO
}



function additems_tonested(oArgs){
	var parentid = oArgs.td.parentNode.id;
	if(posent_cache != null && posent_cache.length > 0 && pos_yui_table_instance != null){
		var rsln = posent_cache.length;
		for(var i=0;i<rsln;i++){
				var rec = pos_yui_table_instance.getRecordSet().getRecord(parentid);
				var onum = rec.getData("orderNumber");
				
				if(onum == posent_cache[i].orderNumber && posent_cache[i].orderLineItems != null && posent_cache[i].orderLineItems.length > 0 && !(posent_cache[i].lineitems_added == "ADDED")){
					rec._oData.__NESTED__.dt.addRows(posent_cache[i].orderLineItems);
					rec._oData.__NESTED__.dt.render();
					posent_cache[i].lineitems_added = "ADDED";
					break;
				}
			}
	}
}

var pos_yui_table_instance = null;
var pos_yui_table_instance_forchnage = null;
var poscolumnDefs = [ {
	key : "orderNumber",
	label : "Order#",
	formatter : "hlinkFormatter",
	sortable : true,
	sortOptions:{sortFunction:sortByPONum}
}, {
	key : "clientNameText",
	label : "Supplier",
	sortable : true
}, {
	key : "orderDate",
	label : "Date",
	formatter: "date",
	sortable : true
} , {
	key : "orderAmount",
	label : "Amount",
	formatter: "currency"
} , {
	key : "versionNumber",
	label : "Ver"
}];
function create_pos_results_table(instanceid, divid,coldefs) {
	if(instanceid == null){


	var posDataSource = new YAHOO.util.DataSource();
	posDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
	 var posConfigs = {
             paginator: new YAHOO.widget.Paginator({
                 rowsPerPage: 2
                 
               
              }),
             initialRequest: "results=504",
             width: "600px",
             // we'll leave the actual loading for later
			initialLoad:true, 
			sortedBy:{key:"orderNumber", dir:"desc"},
				
			// This function links the master to the nested tables
			generateNestedRequest: function (record) {
				return lineItemData;

			}
     };
	 
	 if(divid == "open_send_POTable"){
		 posConfigs = {
	             paginator: new YAHOO.widget.Paginator({
	            	 	 
	                	 rowsPerPage: 20,
	                	 rowsPerPageOptions:[10,25,50,100]
	                	
	              }),
	             initialRequest: "results=504",
	             width: "600px",
	             // we'll leave the actual loading for later
				initialLoad:true, 
				sortedBy:{key:"orderNumber", dir:"desc"},
					
				// This function links the master to the nested tables
				generateNestedRequest: function (record) {
					return lineItemData;
	
				}
	     };
	 }
	 
	/* pos_yui_table_instance = new YAHOO.widget.NestedDataTable(
				'posent_yui_table', 
				poscolumnDefs,
				posDataSource,
				lineItemReadOnlycolumnDefs,
				lineItemDataSource,
				posConfigs);*/
				
	 instanceid = new YAHOO.widget.DataTable(divid,coldefs,posDataSource,posConfigs);
	 instanceid.set("selectionMode", "single");
	 instanceid.subscribe("rowClickEvent",
			 instanceid.onEventSelectRow);
	 instanceid.subscribe("radioClickEvent", function(oArgs){
		 var elRadio = oArgs.target;
         var oRecord = this.getRecord(elRadio);
         poxml_to_open = oRecord._oData.orderXml;
       	  
     });
	}
	//pos_yui_table_instance.subscribe("nestedToggleEvent",additems_tonested); 
	return instanceid;
}

var poxml_to_open = null;

function clear_pos_results_table(instanceid) {
	if (instanceid != null) {
		var length = instanceid.getRecordSet().getLength();
		if (length > 0) {
			instanceid.deleteRows(0, length);
		}
	}
}

function open_po_change(){
	isNotFromDashboard = true;
	try{
	$("open_send_POTable_div").style.display = "block";
	$("open_send_InvoiceTable_div").style.display = "none";
	$("open_send_CreditTable_div").style.display = "none";
	$("open_send_PaymentTable_div").style.display = "none";
	u_hideAllSections();
	u_safeShowElement('openDocument_div');
	}catch(ex){}
	
	if(!isNavigationFromBackButton){
	var functionPointerObj = new Object();
	functionPointerObj.functionName = "open_po_change()";
	navigationStateArray.push(functionPointerObj);
	}
}